home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CPPTASK.ARJ / TSKMSG.CPP < prev    next >
C/C++ Source or Header  |  1991-08-21  |  3KB  |  141 lines

  1. /*
  2.    CPPTask - A Multitasking Kernel For C++
  3.  
  4.    Version 1.0 08-12-91
  5.  
  6.    Ported by Rich Smith from:
  7.  
  8.    Public Domain Software written by
  9.       Thomas Wagner
  10.       Patschkauer Weg 31
  11.       D-1000 Berlin 33
  12.       West Germany
  13.  
  14.    TSKMSG.CPP - Message handling routines.
  15.  
  16.    Subroutines:
  17.         mailbox::mailbox
  18.         mailbox::~mailbox
  19.         mailbox::send_mail
  20.         mailbox::wait_mail
  21.         mailbox::c_wait_mail
  22.         mailbox::check_mailbox
  23.  
  24. */
  25.  
  26. #include <stdio.h>
  27.  
  28. #include "task.hpp"
  29. #include "tsklocal.hpp"
  30.  
  31.  
  32. /*
  33.    mailbox - initialises mailbox.
  34. */
  35.  
  36. mailbox::mailbox ()
  37. {
  38.    waiting = NULL;
  39.    mail_first = mail_last = NULL;
  40. }
  41.  
  42.  
  43. /*
  44.    ~mailbox - kills all processes waiting for mail
  45. */
  46.  
  47. mailbox::~mailbox ()
  48. {
  49.    CRITICAL;
  50.  
  51.    C_ENTER;
  52.    tsk_kill_queue (&waiting);
  53.    mail_first = mail_last = NULL;
  54.    C_LEAVE;
  55.  
  56. }
  57.  
  58.  
  59. /*
  60.    wait_mail - Wait until mail arrives. If there is mail in the box on
  61.                entry, the first mail block is assigned to the caller,
  62.                and the task continues to run.
  63. */
  64.  
  65. farptr far mailbox::wait_mail (dword timeout)
  66. {
  67.    msgptr msg;
  68.    CRITICAL;
  69.  
  70.    C_ENTER;
  71.    if ((msg = mail_first) != NULL)
  72.       {
  73.       if ((mail_first = msg->next) == NULL)
  74.          mail_last = NULL;
  75.       C_LEAVE;
  76.       return msg;
  77.       }
  78.  
  79.    tsk_wait (&waiting, timeout);
  80.    return tsk_current->get_retptr();
  81. }
  82.  
  83. /*
  84.    c_wait_mail - If there is mail in the box on entry, the first mail 
  85.                  block is assigned to the caller, else an error is returned.
  86. */
  87.  
  88. farptr far mailbox::c_wait_mail (void)
  89. {
  90.    msgptr msg;
  91.    CRITICAL;
  92.  
  93.    C_ENTER;
  94.    if ((msg = mail_first) != NULL)
  95.       if ((mail_first = msg->next) == NULL)
  96.          mail_last = NULL;
  97.    C_LEAVE;
  98.    return msg;
  99. }
  100.  
  101.  
  102. /*
  103.    send_mail - Send a mail block to a mailbox. If there are tasks waiting
  104.                for mail, the first waiting task is assigned the block and
  105.                is made eligible.
  106. */
  107.  
  108. void far mailbox::send_mail (farptr msg)
  109. {
  110.    tcbptr curr;
  111.    CRITICAL;
  112.  
  113.    C_ENTER;
  114.    if ((curr = waiting) == NULL)
  115.       {
  116.       if (mail_first == NULL)
  117.          mail_first = (msgptr)msg;
  118.       else
  119.          mail_last->next = (msgptr)msg;
  120.       ((msgptr)msg)->next = NULL;
  121.       mail_last = (msgptr)msg;
  122.       C_LEAVE;
  123.       return;
  124.       }
  125.    waiting = curr->tsk_runable ();
  126.    curr->set_retptr(msg);
  127.    C_LEAVE;
  128. }
  129.  
  130.  
  131. /*
  132.    check_mailbox - returns TRUE if there is mail in the box.
  133. */
  134.  
  135. int far mailbox::check_mailbox (void)
  136. {
  137.    return mail_first != NULL;
  138. }
  139.  
  140.  
  141.